<?php
/**
 * @name /lib/private/NeededItem.php
 * @author Kimberly Berger <knb4850@esu.edu>
 * @version 2011.03.30
 *
 * A NeededItem class to manage NeededItems
 *
 */

ReqLib('/lib/private/Item.php');
ReqLib('/lib/private/Orphanage.php');

/**
 * NeededItem class.
 *
 */
class NeededItem {
  /**
   * NeededItem identification.
   *
   * @var integer
   */
  var $NeededItemId;
  /**
   * NeededItem notes.
   *
   * @var string
   */
  var $NeededNotes;
    /**
   * NeededItem quantity.
   *
   * @var integer
   */
  var $NeededQuantity;
   /**
   * NeededItem DateRequested.
   *
   * @var string
   */
  var $DateRequested;
   /**
   * NeededItem quantity sent.
   *
   * @var integer
   */
   var $QuantitySent;
   /**
   * Orphanage Id for NeededItem
   *
   * @var integer
   */
    var $OrphanageObject;
   /**
   * NeededItem Item object.
   *
   * @var ITEM
   */
   var $ItemObject;

  /* Methods:

  NeededItem()

  "GET":
  NeededItem[] GetNeededItems(int offset, int maxResults)
  bool Exists(int NeededItemId)
  string ToString()

  "SCUD":
  bool Load(int NeededItemId)
  bool Create($ItemId, $OrphanageId)
  bool Save()
  static bool Delete(int NeededItemId)

  Custom:
  GetNeededItemIdsByItemId(ItemId)
  GetNeededItemIdsByOrphanageId(OrphanageID)
  GetNeededItemIdsByOrphanageAndItemId($OrphanageId, $ItemId)
  */

  /**
  *
  * Constructor.
  *
  */
  function NeededItem(){
		$this->NeededItemId = 0;
        $this->NeededQuantity = 0;
		$this->NeededNotes = '';
		$this->DateRequested = '';
        $this->QuantitySent = 0;
        $this->ItemObject = new Item();
		$this->OrphanageObject = new Orphanage();
  }
   /**
   * Generates a list of NeededItems.
   *
   * @param
   * @return
   */
  function GetNeededItems($offset = 0, $maxResults = 0){
		$objects = array();
		$db = new DB();
		// Initial query
		$query = "SELECT NeededItemId FROM neededitems ORDER BY NeededItemId";
		// Look for max results and offsets
		if ($maxResults && $maxResults * 1 == $maxResults) {
			$query .= " LIMIT $maxResults";
			if ($offset && $offset * 1 == $offset)
				$query .= " OFFSET $offset";
		}
		elseif ($offset && $offset * 1 == $offset)
			$query .= " LIMIT 99999999 OFFSET $offset";
		// Get the ids
		$ids = $db->QueryCol($query);
		// If we have ids, load the objects
		if (is_array($ids))
		foreach ($ids as $id) {
			$objects[count($objects)] = new NeededItem();
			$objects[count($objects) - 1]->Load($id);
		}
		return $objects;
   }

    /**
   * Determines if a item exists or not with an neededitemid
   * returns true if it is in the database, false otherwise
   *
   * @param integer
   * @return boolean
   */
  function Exists($NeededItemId){
        $db = new DB();
        return ($db->QueryVal("SELECT NeededItemId FROM neededitems WHERE NeededItemId='" . addslashes($NeededItemId) . "' LIMIT 1;") > 0);
  }
  /**
    * Lists the attributes about a needed item, and returns a string of information
	*
    * @param optional delimiter value
    * @return string
    */
    function ToString($del = "<br / >" ){
		$str = "";
		$str .= 'NeededItemId = ' . $this->NeededItemId . $del;
		$str .= 'NeededQuantity = ' . $this->NeededQuantity . $del;
		$str .= 'Notes = ' .$this->NeededNotes . $del ;
		$str .= 'DateRequested = ' . $this->DateRequested . $del;
		$str .= 'QuantitySent = ' . $this->QuantitySent . $del;
		$str .=  Item::ToString("\r\n") . $del;
        $str .=  Orphanage::ToString("\r\n") . $del;

		return $str;
   }

   /**
  *
  * Loads the NeededItem  using the neededitemId
  * Returns a true on success; a false on failure
  *
  *  @param integer
  *  @return boolean
  */
    function Load($neededItemId){
		if(!NeededItem::Exists($neededItemId)) //can't load something that doesn't exists
             return false;
		else{ //attempt to load the item
            $db = new DB();
			$this->ItemObject = new Item();
			$this->OrphanageObject = new Orphanage();
			$data = $db->QueryRow("SELECT NeededItemId, ItemId, Quantity, OrphanageId, Notes, DateRequested, QuantitySent FROM neededitems Where NeededItemId = '" . addslashes($neededItemId) . "';");
                $this->NeededItemId = $data["NeededItemId"];
                $this->NeededQuantity = $data["Quantity"];
                $this->NeededNotes = $data["Notes"];
                $this->DateRequested = $data["DateRequested"];
                $this->QuantitySent = $data["QuantitySent"];

				if(!$this->ItemObject->Load($data["ItemId"]))
					return false;
				if(!$this->OrphanageObject->Load($data["OrphanageId"]))
					return false;
			return true;
            }
	}

  /**
   * Creates a Neededitem using an itemId and an orphanageId
   * returns true on success, false on failure.
   *
   * @return boolean
   */
	function Create($itemId, $orphanageId){
		if(Item::Exists($itemId) && Orphanage::Exists($orphanageId)){ //make sure the item and orphanage both don't create  a neededitem with something that doesn't exists
		$db = new DB();
		// Insert and store details
        $sql = "INSERT INTO neededitems (ItemId, OrphanageId) VALUES ('" . addslashes($itemId) ."', '" .addslashes($orphanageId) . "');";
        $db->Query($sql);
        $sql = "SELECT LAST_INSERT_ID();";
        $this->NeededItemId = $db->QueryVal($sql);
        $this->OrphangeObejct = Orphanage::Load($orphanageId);
        $this->ItemObject = Item::Load($itemId);
        return true;
		}
		else
			return false;
	}
  /**
  * Saves a Neededitem
  * returns true on success, false on failure.
  *
  * @return boolean
  */
  function Save(){
    if (!NeededItem::Exists($this->NeededItemId)) //make sure you aren't trying to save a neededitem that doesn't exists
            return false;
    else{
		if(Item::Exists($this->ItemId) && Orphanage::Exists($this->OrphanageId)){ // check to make sure any changes to the itemid and orphanageid so that they actually exists
			$db = new DB();
			$data = $db->QueryRow("UPDATE neededitems
                SET ItemId='" . addslashes($this->ItemId) . "' ,
                Quantity='" . addslashes($this->NeededQuantity) . "' ,
                OrphanageId = '". addslashes($this->OrphanageId)."' ,
                Notes='" .addslashes($this->NeededNotes) . "' ,
                QuantitySent ='".addslashes($this->QuantitySent)."' ,
                DateRequested='" .addslashes($this->DateRequested)."'
                Where NeededItemId='" .addslashes($this->NeededItemId) ."';");

			/*$ItemObject = new Item();

			if (!$this->ItemObject->Load($data["ItemId"]))
				return false;

			$OrphanageObject = new Orphanage();

			if(!$this->OrphanageObject->Load($data["OrphanageId"]))
				return false;
			return true;*/
		}
		else
			return false;
    }
  }

  /**
   * Deletes a neededitem and takes a neededitemid
   * returns true on success, false on failure.
   *
   * @param integer
   * @return boolean
   */
  function Delete($NeededItemId){
    if(!NeededItem::Exists($NeededItemId)) //don't try to delete something that doesn't exist
        return false;
    else{
        $db = new DB();
		// delete the neededitem
        $db->Query("DELETE FROM neededitems WHERE NeededItemId ='".addslashes($NeededItemId)."';");
        return true;
    }
  }
  /**
  * STATIC METHOD
  * Gets the $neededItemIds by using the $ItemId.
  * Returns an array of neededitemids
  *
  *  @param integer
  *  @return integer[]
  */
   function GetNeededItemIdsByItemId($itemId){
		$neededItemIds = array(); // create an array
        $db = new DB();
        $neededItemIds = $db->QueryCol("SELECT NeededItemId FROM neededitems Where ItemId='" . addslashes($itemId) . "' ORDER BY NeededItemId;"); //fill the array
		return $neededItemIds;
   }

   /**
  * STATIC METHOD
  * Gets the $neededItemIds by using the $OrphanageId.
  * Returns an array of neededItemIds
  *
  *  @param integer
  *  @return integer[]
  */
   function GetNeededItemIdsByOrphanageId($orphanageId){
		$neededItemIds = array(); //create an array
        $db = new DB();
        $neededItemIds = $db->QueryCol("SELECT NeededItemId FROM neededitems Where OrphanageId='" . addslashes($orphanageId) . "' ORDER BY NeededItemId;"); //fill the array
        return $neededItemIds;
   }

  /**
  * STATIC METHOD
  * Gets the $neededItemIds by using the $OrphanageId and $ItemId
  * Returns an array of neededitemids
  *
  *  @param integer
  *  @return integer[]
  */
   function GetNeededItemIdsByOrphanageAndItemId($orphanageId, $itemId){
	   $neededItemIds = array();//create an array
       $db = new DB();
       $neededItemIds = $db->QueryCol("SELECT NeededItemId FROM neededitems Where ItemId='" . addslashes($itemId) . "' AND OrphanageId ='" .addslashes($orphanageId)." ORDER BY NeededItemId';"); //fills the array
       return $neededItemIds;
	}
}